/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[ParsingMails.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.mail;

import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

import org.junit.Before;
import org.junit.Test;

import javamail.utils.MailAuthenticator;
import javamail.utils.MailConstants;
import javamail.utils.MailUtils;
import javamail.utils.PasswordUtils;

/**
 * 解析邮件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class ParsingMails {

	private static Properties properties = null;

	private static String emailAddress = "javamail_sns@sina.com";

	@Before
	public void init() {
		properties = new Properties();
		properties.put("mail.store.protocol", MailConstants.PROTOCAL_POP3);
		properties.put("mail.pop3.host", MailConstants.POP3_HOST_OF_SINA);
		properties.put("mail.pop3.port", MailConstants.POP3_PORT_OF_SINA);
		properties.put("mail.pop3.auth", MailConstants.IS_AUTHORIZATION_NEED);
		properties.put("mail.debug", String.valueOf(MailConstants.IS_DEBUG_MOD_ENABLED));
	}

	/**
	 * 解析邮件
	 * 
	 * @throws Exception
	 */
	public static void parseMails() throws Exception {
		// 创建Session对象并连接到指定的邮箱
		Session session = Session.getInstance(properties,
				new MailAuthenticator(emailAddress, PasswordUtils.readPassword(emailAddress)));
		Store store = session.getStore();
		store.connect(emailAddress, PasswordUtils.readPassword(emailAddress));

		// 获得收件箱
		Folder folder = store.getFolder("INBOX");
		// 以只读权限打开收件箱, 还可以配置为Folder.READ_WRITE
		folder.open(Folder.READ_ONLY);

		// 得到收件箱里所有的邮件
		Message[] messages = folder.getMessages();

		// 解析邮件
		for (Message message : messages) {
			System.out.println("第" + message.getMessageNumber() + "封邮件 ");
			System.out.println("邮件主题:     " + MailUtils.getMailSubject(message));
			System.out.println("邮件发件人:   " + MailUtils.getMailSender(message));
			System.out.println("邮件收件人:   " + MailUtils.getMailRecipients(message, null));
			System.out.println("邮件发送时间: " + MailUtils.getMailSendDate(message, null));
			System.out.println("邮件是否已读: " + MailUtils.isSeen(message));
			System.out.println("邮件的优先级: " + MailUtils.getMailPriority(message));
			System.out.println("是否需要回执: " + MailUtils.isReplaySign(message));
			System.out.println("邮件的大小:   " + message.getSize() * 1024 + "KB");
			boolean containerAttachment = MailUtils.containsAttachment(message);
			System.out.println("是否包含附件: " + containerAttachment);
			if (containerAttachment) {
				MailUtils.saveAttachment(message, "Z:/temp/" + message.getSubject() + "_");
			}
			StringBuilder content = new StringBuilder(30);
			MailUtils.getMailTextContent(message, content);
			System.out.println("邮件正文: " + (content.length() > 100 ? content.subSequence(0, 100) + "......" : content));
			System.out.println();
		}

		folder.close(true);
		store.close();
	}

	@Test
	public void testParseMails() throws Exception {
		parseMails();
	}
}
